#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Evaluate flags                                                     #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 12/12/2018                                             #
# Last Modification: 12/12/2018                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 84 
#
# MANUAL: This script will set image flags for this image
#
# DISPLAY: import_drift
# DISPLAY: iciness
# DISPLAY: defocus_RESMAX 
# DISPLAY: defocus_defocus
# DISPLAY: defocus_astig
# DISPLAY: defocus_phase_shift
# DISPLAY: defocus_CCvalue
# DISPLAY: frame_image_counts
# DISPLAY: import_original_time
# DISPLAY: flag_isdark 
# DISPLAY: flag_hasstripe 
# DISPLAY: flag_hastoohighdefocus 
# DISPLAY: flag_hastoolowdefocus 
# DISPLAY: flag_icinesstoohigh 
# DISPLAY: flag_drifttoohigh 
# DISPLAY: flag_astigmatismtoohigh 
# DISPLAY: flag_ctfresolutiontoobad 
# DISPLAY: flag_local_isdark_threshold
# DISPLAY: flag_local_hasstripe_threshold 
# DISPLAY: flag_local_hastoohighdefocus_threshold 
# DISPLAY: flag_local_hastoolowdefocus_threshold 
# DISPLAY: flag_local_icinesstoohigh_threshold 
# DISPLAY: flag_local_drifttoohigh_threshold 
# DISPLAY: flag_local_astigmatismtoohigh_threshold
# DISPLAY: flag_local_ctfresolutiontoobad_threshold
# DISPLAY: notify_local_last_minutes 
# DISPLAY: notify_local_delay_minutes
# DISPLAY: notify_local_lastnumber 
# DISPLAY: notify_local_limit_mild
# DISPLAY: notify_local_limit_severe
# DISPLAY: image_stddev
# DISPLAY: image_stddev_relative
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set import_original_time = ""
set import_drift = ""
set iciness = ""
set defocus_RESMAX = ""
set defocus_defocus = ""
set defocus_astig = ""
set defocus_phase_shift = ""
set defocus_CCvalue = ""
set frame_image_counts = ""
set flag_isdark = ""
set flag_hasstripe = ""
set flag_hastoohighdefocus = ""
set flag_hastoolowdefocus = ""
set flag_icinesstoohigh = ""
set flag_drifttoohigh = ""
set flag_astigmatismtoohigh = ""
set flag_ctfresolutiontoobad = ""
#
set flag_local_isdark_threshold = ""
set flag_local_hasstripe_threshold = ""
set flag_local_hastoohighdefocus_threshold = "" 
set flag_local_hastoolowdefocus_threshold = "" 
set flag_local_icinesstoohigh_threshold = "" 
set flag_local_drifttoohigh_threshold = "" 
set flag_local_astigmatismtoohigh_threshold = ""
set flag_local_ctfresolutiontoobad_threshold = ""
set notify_local_last_minutes = "" 
set notify_local_delay_minutes = ""
set notify_local_lastnumber = "" 
set notify_local_limit_mild = ""
set notify_local_limit_severe = ""
set image_stddev = ""
set image_stddev_relative = ""
set movie_stackname = ""
#
set raw_gaincorrectedstack = ""
#
#$end_vars
#
set scriptname = process_evaluate_flags
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
echo "<<@evaluate>>"
#
echo "<<@progress: 1>>"
#
echo ":: "
echo ":: Thresholds defined in the Preferences dialogue are:"
echo ":: "
echo ":: flag_isdark_threshold = ${flag_isdark_threshold}"
echo ":: flag_hasstripe_threshold = ${flag_hasstripe_threshold}"
echo ":: flag_hastoohighdefocus_threshold = ${flag_hastoohighdefocus_threshold}"
echo ":: flag_hastoolowdefocus_threshold = ${flag_hastoolowdefocus_threshold}"
echo ":: flag_icinesstoohigh_threshold = ${flag_icinesstoohigh_threshold}"
echo ":: flag_drifttoohigh_threshold = ${flag_drifttoohigh_threshold}"
echo ":: flag_astigmatismtoohigh_threshold = ${flag_astigmatismtoohigh_threshold}"
echo ":: flag_ctfresolutiontoobad_threshold = ${flag_ctfresolutiontoobad_threshold}"
echo ":: "
echo ":: notify_last_minutes = ${notify_last_minutes} minutes"
echo ":: notify_lastnumber = ${notify_lastnumber}"
echo ":: notify_limit_mild = ${notify_limit_mild}"
echo ":: notify_limit_severe = ${notify_limit_severe}"
echo ":: "
echo ":: Microscope_Name = ${Microscope_Name}"
echo ":: "
#
echo "# IMAGE-IMPORTANT: ${movie_stackname}_Sum.mrc <DriftCor image (2D, no DW)>" >> LOGS/${scriptname}.results
#
echo "<<@progress: 30>>"
#
echo "Testing frame image counts, if ${frame_image_counts} < ${flag_isdark_threshold}"
# echo ":: Frame Image Counts is ${frame_image_counts}"
set retval = `echo x${frame_image_counts} | sed 's/\./x/g'`
# echo ":: Output is ${retval}"
if ( "${retval}" == "xx" ) then
  echo "Frame Image Counts not determined."
else
  set retval = `echo ${frame_image_counts} ${flag_local_isdark_threshold} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${retval} == "1" ) then
    ${proc_2dx}/linblock "Frame image counts too low."
    set flag_isdark = "y"
    echo "set flag_isdark = ${flag_isdark}" >> LOGS/${scriptname}.results
  endif
endif
#
if ( -e ${raw_gaincorrectedstack}.mrc ) then
  set inputfile = ${raw_gaincorrectedstack}.mrc 
else if ( -e ${movie_stackname}_Sum.mrc ) then
  set inputfile = ${movie_stackname}_Sum.mrc
else 
  set inputfile = "none"
endif
echo ":inputfile = ${inputfile}"
echo "# IMAGE: ${inputfile} <Input file for standard deviation calculation>" >> LOGS/${scriptname}.results
if ( ${inputfile} != "none" ) then
  echo ":Calculating standard deviation from file ${inputfile}"
  set tmpstring = `clip stats ${inputfile} | head -n 3 | tail -n 1`
  set tmpstring2 = `echo ${tmpstring}`
  set val_min = `echo ${tmpstring2} | cut -d\  -f 2`
  set val_max = `echo ${tmpstring2} | cut -d\)  -f 2 | cut -d\( -f 1`
  set val_diff = `echo ${val_max} ${val_min} | awk '{ s = abs ( $1 - $2 ) } END { print s }'`
  set val_average = `echo ${tmpstring2} | rev | cut -d\  -f 2 | rev`
  set image_stddev = `echo ${tmpstring2} | rev | cut -d\  -f 1 | rev`
  echo "min = ${val_min}, max = ${val_max}, diff = ${val_diff}, average = ${val_average}, stddev = ${image_stddev}"
  echo "set image_stddev = ${image_stddev}" >> LOGS/${scriptname}.results
  set is_num = `echo ${val_diff} | awk '{ if ( $1 > 0.0001 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${is_num} == 1 ) then
    set image_stddev_relative = `echo ${image_stddev} ${val_diff} | awk '{ s = int ( $1 * 100000 / $2 ) / 1000 } END { print s }'`
  else
    set image_stddev_relative = 0.0
  endif
  echo "set image_stddev_relative = ${image_stddev_relative}" >> LOGS/${scriptname}.results
  set val_isbad = `echo ${image_stddev_relative} ${flag_local_hasstripe_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  echo "Testing stddev, if ${image_stddev_relative} > ${flag_local_hasstripe_threshold}"
  if ( ${val_isbad} == "1" ) then
    ${proc_2dx}/linblock "Standard deviation is too high. Average = ${val_average}, StdDev = ${image_stddev}, relative StdDev = ${image_stddev_relative}, threshold = ${flag_local_hasstripe_threshold}."
    set flag_hasstripe = "y"
  else
    set flag_hasstripe = "n"
  endif
  echo "set flag_hasstripe = ${flag_hasstripe}" >> LOGS/${scriptname}.results
endif
#
echo "Testing defocus, if ${defocus_defocus} > ${flag_local_hastoohighdefocus_threshold}"
set retval = `echo ${defocus_defocus} ${flag_local_hastoohighdefocus_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "Defocus too high."
  set flag_hastoohighdefocus = "y"
else
  set flag_hastoohighdefocus = "n"
endif
echo "set flag_hastoohighdefocus = ${flag_hastoohighdefocus}" >> LOGS/${scriptname}.results

#
echo "Testing defocus, if ${defocus_defocus} < ${flag_local_hastoolowdefocus_threshold}"
set retval = `echo ${defocus_defocus} ${flag_local_hastoolowdefocus_threshold} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "Defocus too low."
  set flag_hastoolowdefocus = "y"
else
  set flag_hastoolowdefocus = "n"
endif
echo "set flag_hastoolowdefocus = ${flag_hastoolowdefocus}" >> LOGS/${scriptname}.results
#
echo "Testing iciness, if ${iciness} > ${flag_local_icinesstoohigh_threshold}"
set retval = `echo ${iciness} ${flag_local_icinesstoohigh_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "Iciness too high."
  set flag_icinesstoohigh = "y"
else
  set flag_icinesstoohigh = "n"
endif
echo "set flag_icinesstoohigh = ${flag_icinesstoohigh}" >> LOGS/${scriptname}.results
#
echo "Testing drift, if ${import_drift} > ${flag_local_drifttoohigh_threshold}"
set retval = `echo ${import_drift} ${flag_local_drifttoohigh_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "Drift too high."
  set flag_drifttoohigh = "y"
else
  set flag_drifttoohigh = "n"
endif
echo "set flag_drifttoohigh = ${flag_drifttoohigh}" >> LOGS/${scriptname}.results
#
echo "Testing astigmatism, if ${defocus_astig} > ${flag_local_astigmatismtoohigh_threshold}"
set retval = `echo ${defocus_astig} ${flag_local_astigmatismtoohigh_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "Astigmatism too high."
  set flag_astigmatismtoohigh = "y"
else
  set flag_astigmatismtoohigh = "n"
endif
echo "set flag_astigmatismtoohigh = ${flag_astigmatismtoohigh}" >> LOGS/${scriptname}.results
#
echo "Testing CTF resolution, if ${defocus_RESMAX} > ${flag_local_ctfresolutiontoobad_threshold}"
set retval = `echo ${defocus_RESMAX} ${flag_local_ctfresolutiontoobad_threshold} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${retval} == "1" ) then
  ${proc_2dx}/linblock "CTF resolution too bad."
  set flag_ctfresolutiontoobad = "y"
else
  set flag_ctfresolutiontoobad = "n"
endif
echo "set flag_ctfresolutiontoobad = ${flag_ctfresolutiontoobad}" >> LOGS/${scriptname}.results
#
#
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
exit
#
# These are listed here to make sure they appear in the 2dx_image GUI:
#
